package com.gduf.lsm.controller;

import com.gduf.lsm.common.Result;
import com.gduf.lsm.entity.User;
import com.gduf.lsm.pojo.dto.MoneyDTO;
import com.gduf.lsm.pojo.dto.VoluTimeDTO;
import com.gduf.lsm.service.UserService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * @Author lsm
 * @Data
 */

/**
 * 用户前端操作接口
 **/
@RestController
@RequestMapping("/user")

public class UserController {
    @Resource
    private UserService userService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        userService.add(user);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        userService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        userService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody User user) {
        userService.updateById(user);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        User user = userService.selectById(id);
        return Result.success(user);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(User user ) {
        List<User> list = userService.selectAll(user);
        return Result.success(list);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(User user,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<User> page = userService.selectPage(user, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 添加金额
     * @param moneyDTO
     * @return
     */
    @PutMapping("/updateMoney")
    public Result updateMoney(@RequestBody MoneyDTO moneyDTO){
        userService.updateMoney(moneyDTO);
        return Result.success();
    }

    /**
     * 处理志愿时长
     * @param voluTimeDTO
     * @return
     */
    @PutMapping("/updateVoluTime")
    public Result updateVoluTime(@RequestBody VoluTimeDTO voluTimeDTO){
        userService.updateVoluTime(voluTimeDTO);
        return Result.success();
    }
}
